package com.feng.study.config;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

@Component
@Log4j2
public class ShutdownCommandLineRunner implements CommandLineRunner {

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void run(String... args) throws Exception {
        log.info("...........");
        // 优雅停机，默认20秒后停机，可通过在application.yml或环境变量等配置estaGracefulShutdownWaitSeconds参数进行调整
        Runtime.getRuntime().addShutdownHook(new Thread(new GracefulNacosDeregister((ConfigurableApplicationContext) applicationContext)));
    }
}
